library(tidyverse)
library(jpeg)
library(readxl)
library(here)
library(brickr)
library(rayshader)
## Warning in rgl.init(initValue, onlyNULL): RGL: unable to open X11 display
## Warning: 'rgl.init' failed, running with 'rgl.useNULL = TRUE'.
library(rgl)
library(circlepackeR)
library(data.tree)
library(treemap)
library(networkD3)
library(RColorBrewer)
library(ggraph)


rgl::setupKnitr()
#a cute little house!

#read_xlsx("~/Iggy-Izzy/data/brickr_pattern.xlsx", sheet = "mybrickr") %>% 
 # bricks_from_excel() %>% 
#  build_bricks(rgl_lit = FALSE, outline_bricks = TRUE,
 #              background_color =  "#99e7ff")

#par3d(userMatrix = rotate3d(par3d("userMatrix"), .1*pi, 0, 0 ,1))

#par3d(windowRect = c(20, 30, 900, 800))
## wranglin 


smallthemes <- themes %>%
  select(id, name) %>%
  rename("theme_id"=id)

## dataset containing sets, themes, and number of parts 

sets_themes <- sets %>%
  filter(num_parts >= 40) %>%
  left_join(smallthemes, by="theme_id") %>%
  rename("set_name"=name.x, "theme_name"=name.y)

The data we used to create the following graphs comes from the Rebrickable website. here Rebrickable allows users to input different lego pieces and gives them instructions to create sets out of the pieces they already own. This means that Rebrickable has tons and tons of data on legos, literally twelve different CSV’s of colors, size, part, theme, anything you could possibly want to know about an individual lego or set. Rebrickable has all of their data available for free on their website or through their API. The data was luckily in a tidy format (yay!) and using the handy schema we were able to combine data sets containing the information we desired. To create the radial networks and other graphs we desired the data needed to be formatted into a data tree. Luckily, the data.tree package has just the tools to make that happen! A data tree is a made up of nodes, where each node is a data structure consisting of a value. In a couple of our data trees the ‘root’ node is legos and then from there the ‘children’ nodes are themes, sets, and maybe the number of parts. The data tree is non linear in the sense that none of the ‘child’ nodes can point back to the ‘root’ node. The data.tree package allowed us to easily create these nested data frames using the paste function, the $ operator, and the as.node function.

## converting the data.frame to a data.tree structure
sets_themes$pathString <- paste("legos", 
                            sets_themes$theme_name,  
                            sets_themes$set_name,
                            sep = "/")
## Selecting by n
## data tree structure! Neet!
large_sets <- as.Node(large_sets) 
print(large_sets, limit = 20)
##                                             levelName
## 1  legos                                             
## 2   ¦--Basic Set                                     
## 3   ¦   ¦--25th Anniversary Silver Tub               
## 4   ¦   ¦--XXL 2000 Canister                         
## 5   ¦   ¦--Build Your Own House Tub                  
## 6   ¦   ¦--35th Anniversary Tub                      
## 7   ¦   ¦--Large Creator Bucket                      
## 8   ¦   ¦--Blue Strata XXL                           
## 9   ¦   ¦--Better Building More Fun                  
## 10  ¦   ¦--Blue Tub                                  
## 11  ¦   ¦--XXL 2000                                  
## 12  ¦   ¦--XXL 1800                                  
## 13  ¦   ¦--Giant Box                                 
## 14  ¦   °--LEGO Deluxe Starter Set                   
## 15  ¦--Construction                                  
## 16  ¦   ¦--Mobile Crane MK II                        
## 17  ¦   ¦--Remote-Controlled Volvo L350F Wheel Loader
## 18  ¦   ¦--Mercedes Benz Arocs 3245                  
## 19  ¦   ¦--Bucket Wheel Excavator                    
## 20  ¦   °--... 7 nodes w/ 0 sub                      
## 21  °--... 7 nodes w/ 82 sub
 # within the top 10 themes these are the 100 sets with the most pieces. 
    large_sets_list <- ToListExplicit(large_sets, unname = TRUE)
    
## the best radial network 
    radialNetwork(large_sets_list)

Once our data trees were created we could start graphing (yay!). At the center of the radial network is legos, from there the first set of nodes is the top ten lego themes by number of occurrences, and each the secondary set of nodes is of the top ten themes the hundred largest sets. Pursuing through the network we can see that Star Wars is a popular theme with many large sets.

## Selecting by n
##                              levelName
## 1  legos                              
## 2   ¦--Basic Set                      
## 3   ¦   ¦--Creative Suitcase          
## 4   ¦   ¦   °--1017                   
## 5   ¦   ¦--Large Bulk Bucket          
## 6   ¦   ¦   °--950                    
## 7   ¦   ¦--Large Bucket               
## 8   ¦   ¦   °--950                    
## 9   ¦   ¦--25th Anniversary Silver Tub
## 10  ¦   ¦   °--1201                   
## 11  ¦   ¦--XXL 2000 Canister          
## 12  ¦   ¦   °--2000                   
## 13  ¦   ¦--Build Your Own House Tub   
## 14  ¦   ¦   °--1201                   
## 15  ¦   ¦--35th Anniversary Tub       
## 16  ¦   ¦   °--1200                   
## 17  ¦   ¦--Create and Imagine         
## 18  ¦   ¦   °--1000                   
## 19  ¦   ¦--Build Your Dreams          
## 20  ¦   ¦   °--... 1 nodes w/ 0 sub   
## 21  ¦   °--... 15 nodes w/ 17 sub     
## 22  °--... 4 nodes w/ 180 sub

The largest circle in this figure represents legos i.e the tree root , then the next set of circles is the top five themes within legos. Within each theme are the largest sets and clicking on an individual set’s circle you can see the number of parts within that set. This graph is limited to the hundred largest lego sets within those top five themes. With the circlepackeR and the networkD3 packages we found ourselves wanting to change some of the characteristics of the graphs such as font size and spacing and it was slightly infuriating that these were not adjustable within the packages.

The Millennium Falcon

#USC Millennium Falcon, 7541 parts, released 2017
#set num = 75192-1

inventories %>%
  filter(set_num == "75192-1")
##   inventory_id version set_num
## 1        19670       1 75192-1
millenium_falcon <- parts_inventory %>%
  filter(inventory_id == 19670) %>% #inventory ID gotten from previous step
  left_join(colors, by="color_id") %>%
  left_join(parts, by="part_num") %>%
  left_join(parts_categories, by="part_cat_id") %>%
  rename("part_cat"="name", "color_name"="name.x", "part_name"="name.y")
## Warning: Column `part_num` joining factors with different levels, coercing to
## character vector
millenium_falcon$pathString <- paste("mf", 
                            millenium_falcon$color_name, 
                            millenium_falcon$part_cat,
                            millenium_falcon$part_name,
                            millenium_falcon$quantity,
                            sep = "/")

MILF <- as.Node(millenium_falcon)

#color, then part category, then part name, then part quantity
m <- circlepackeR(MILF, size = "quantity", width = 1000, height = 800)

m
millennium_figs <- inventories %>%
  filter(inventory_id == 19670) %>%
  left_join(minifig_inventory, by="inventory_id") %>%
  left_join(minifigs, by="fig_num") %>%
  select(4, 6, 7)
## Warning: Column `fig_num` joining factors with different levels, coercing to
## character vector
millennium_figs
##      fig_num                                       name num_parts
## 1 fig-001714              Chewbacca with Dark Brown Fur         3
## 2 fig-001810                                       Finn         4
## 3 fig-002057                       Rey in Crossed Robes         4
## 4 fig-002514                    C-3PO with Printed Legs         3
## 5 fig-002544                                       BB-8         2
## 6 fig-002549               Princess Leia in Hoth Outfit         4
## 7 fig-002550 Han Solo with Dark Brown Legs with Gunbelt         4
## 8 fig-002551                             Han Solo (Old)         4
knitr::include_graphics(here("milf_minifigs.png"))

#wrangling!
bigcolors <- sets %>%
  filter(year == 2000) %>%
  left_join(inventories, by="set_num") %>%
  left_join(parts_inventory, by="inventory_id") %>%
  left_join(colors, by="color_id") %>%
  filter(num_parts > 0) %>%
  select(2, 3, 10, 12, 13) %>%
  group_by(name.y) %>%
  summarise(n=sum(quantity)) %>%
  arrange(desc(n)) %>%
  rename("color"="name.y", "n_parts"="n")

#treemap
treemap(bigcolors,
            index="color",
            vSize="n_parts",
            type="index"
            )

#dataframe
bigcolors %>%
  head(10)
## # A tibble: 10 x 2
##    color      n_parts
##    <fct>        <int>
##  1 Black         8660
##  2 Light Gray    4962
##  3 Red           3259
##  4 White         2928
##  5 Yellow        2876
##  6 Sand Green    2867
##  7 Dark Gray     2465
##  8 Blue          2372
##  9 Green         1877
## 10 Tan            914
mosaic <- png::readPNG("iggy-izzy.png") %>% 
  image_to_mosaic(img_size = 80) 

mosaic%>% build_mosaic()